package controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.Order;
import pojo.OrderItem;
import pojo.Merchant;
import service.OrderService;

@Controller
public class OrderController {

	@Resource(name="orderServiceImpl")
	private OrderService orderService;
	
	@RequestMapping(value="/views/orders",method=RequestMethod.GET)
	public String findAll(Model model,HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		List<Order> orderItemList = orderService.findAll(merchant.getId());
		model.addAttribute("merchant", merchant);
		model.addAttribute("orderItemList",orderItemList);
		return "WEB-INF/order/orderList";
	}
	
	@ResponseBody
	@RequestMapping(value="/orders/status",method = RequestMethod.GET)
	public List<Order> findByStatus(@RequestParam("order_status_id") String status,HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		System.out.println(status);
		System.out.println(merchant.getId());
		System.out.println("success orderlist");
		List<Order> orderList = orderService.findOrderByStatus(status,merchant.getId());
		return orderList;
	}
	
	@ResponseBody
	@RequestMapping(value="/orders/reply",method = RequestMethod.GET)
	public List<Order> findReplyable(HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		List<Order> orderList = orderService.findByMerchantId(merchant.getId());
		return orderList;
	}
	
	@RequestMapping(value="/orders/detail",method=RequestMethod.GET)
	public String findByProcessNumber(@RequestParam("order_processNumber_id") String processNumber,Model model,HttpSession session){
		System.out.println("success orderItem");
		List<OrderItem> orderItemList = orderService.findOrderItem(processNumber);
		double total = 0;
		for(OrderItem o:orderItemList){
			System.out.println(o.getPrice()+"x"+o.getAmount());
		}
		for(OrderItem o:orderItemList){
			total += o.getPrice()*o.getAmount();
		}
		model.addAttribute("total", total);
		model.addAttribute("orderItemList",orderItemList);
		model.addAttribute("processNumber",processNumber);
		
		Merchant merchant = (Merchant) session.getAttribute("user");
		model.addAttribute("merchant", merchant);
		return "WEB-INF/order/orderDetail";
	}
	
	@ResponseBody
	@RequestMapping(value="/orders/{id}/{status}",method=RequestMethod.PUT)
	public Order update(@PathVariable String id,@PathVariable String status,HttpSession session){
		Merchant merchant = (Merchant) session.getAttribute("user");
		Order order = orderService.updateOrderStatus(id,status,merchant.getId());
		System.out.println(order.getStatus());
		return order;
	}
	
	@ResponseBody
	@RequestMapping(value="/orders/order",method=RequestMethod.GET)
	public Order findByprocessNumber(@RequestParam("order_processNumber_id") String processNumber){
		Order order = orderService.findByprocessNumber(processNumber);
		System.out.println("success procenum"+order.getStatus());
		return order;
	}
}
